package com.zhiwen.design.proxy.cglibProxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Auther: wuzhiwen
 * @Date: 2018/12/3 14:21
 * @Description:
 */
public class CglibSellHouseProxy implements MethodInterceptor {

    private Object target;

    public Object getInstance(Object target) {
        this.target = target;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("有人要卖房子了吗");
        Object invoke = methodProxy.invoke(target, objects);
        System.out.println("我们帮你买了");
        return invoke;
    }
}
